package org.six11.olive3.server;

import java.io.IOException;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import org.six11.util2.Debug;

public class WebActionServlet extends HttpServlet {

  private static final String DATASOURCE_NAME = "jdbc/sketching_games";
  public static final String WEB_ACTION_URI = "/webaction"; // seriously can't I derive this???
  public static final String WEB_ACTION_SHORT = WEB_ACTION_URI.substring(1);

  public static final String PARAM_ACTION = "action";
  public static final String PARAM_DESC_ID = "descID";
  public static final String PARAM_PASS = "pass";
  public static final String PARAM_SKETCH_ID = "sketchID";
  public static final String PARAM_USER = "user";

  public static final String ACTION_WTF = "wtf are you talking about?";
  public static final String ACTION_REGISTER = "reg";
  public static final String ACTION_LOGIN = "login";
  public static final String ACTION_PLAY = "play_picturephone";
  public static final String ACTION_BROWSE = "browse";
  public static final String ACTION_SUGGEST = "suggest";

  public static final String SESSION_USER_NAME = "userName";
  public static final String SESSION_USER_ID = "userID";

  public static final String MSG_ERROR = "errorMsg";
  public static final String MSG_INFO = "infoMsg";

  private Map<String, WebActionHandler> actions;
  protected DatabaseThing dbThing;

  public WebActionServlet() throws NamingException {
    super();
    actions = new HashMap<String, WebActionHandler>();
    actions.put(ACTION_WTF, new WtfHandler("/wtf.jsp", this));
    actions.put(ACTION_REGISTER, new RegisterHandler("/register.jsp", this));
    actions.put(ACTION_LOGIN, new LoginHandler("/login.jsp", this));
    actions.put(ACTION_PLAY, new PlayHandler("/play.jsp", this));
    actions.put(ACTION_BROWSE, new BrowseHandler("/browse.jsp", this));
    actions.put(ACTION_SUGGEST, new SuggestHandler("/suggest.jsp", this));

    Context env = (Context) new InitialContext().lookup("java:comp/env");
    dbThing = new DatabaseThing((DataSource) env.lookup(DATASOURCE_NAME));
  }

  protected Map<String, WebActionHandler> getActions() {
    return actions;
  }

  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
      IOException {
    doPost(req, resp);
  }

  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
      IOException {
    ServletContext cx = getServletContext();
    // if another servlet sent us here, it may have changed the requested action, which is stored as
    // an attribute. Request parameters are immutable, so I had to do it this way.
    String action = (String) req.getAttribute(PARAM_ACTION);
    if (action == null) {
      action = req.getParameter(PARAM_ACTION);
    } else {
      Debug.out("WebActionServlet", "Action overriden from " + req.getParameter(PARAM_ACTION)
          + " to " + action);
    }
    if (actions.containsKey(action)) {
      actions.get(action).service(req, resp, cx);
    } else {
      // If the action is unknown, toss the user to an error page. It is more humane than burning
      // them.
      actions.get(ACTION_WTF).service(req, resp, cx);
    }
  }

  public DatabaseThing getDbThing() {
    return dbThing;
  }

}
